<?php
namespace app\__MODULE__\controller;
use think\facade\Db;
use app\__MODULE__\model\Product as ProductModel;
use app\__MODULE__\model\Exchange;

class Product extends Base{
    

    /**
     * 首页
     */
    public function index(){
        if(IS_POST) {
            $search  = request()->param();
            $where = [];
            if(isset($search["name"]) && $search["name"]!="") $where[] = ["name","=",$search["name"]];

            $db   = ProductModel::where($where)->order('id desc');
            $list = $db->limit(paginator())->select();
             if(count($list)){
                foreach($list as &$v){
                    $v["thumb"] = table_img($v["thumb"]);
                }
}

            __MODULE___response(0, 'success', $list, $db->count());
        }
        if(IS_EXCEL) {
            $db = ProductModel::where($where)->order('id asc');
            big_array_to_csv($db, '数据导出', []);
        }
        return $this->render_table([
           "page"=>true,
            "toolbar" => [table_button('__MODULE__/Product/add',"添加","frame")],
            "toolbar_row" => [ 
                    table_button('__MODULE__/Product/edit',"编辑","frame"),
                    table_button('__MODULE__/Product/del',"删除","confirm")
                    ],
            "cols" => [ 
                     ["type"=>"checkbox"],
                    ["field"=>"name", "title"=>"名称","align"=>"center"],
                    ["field"=>"desc", "title"=>"简介","align"=>"center"],
                    ["field"=>"thumb", "title"=>"图片","align"=>"center"],
                    ["field"=>"stock", "title"=>"库存","align"=>"center"],
                    ["field"=>"out_price", "title"=>"市场价格","align"=>"center"],
                    ["field"=>"score", "title"=>"积分","align"=>"center"],

            ],
            "where"=>[
                    "name"=>["type"=>"input","title"=>"名称"],

            ]

            ],false);

    }



        /**
     * 添加
     * @return void
     * @author 
     * date 
     */
    public function add(){
        if(request()->isPost()) {
           $data = request()->post();
           if(ProductModel::create($data)) json_response(1,'操作成功');
           json_response(0,'操作失败');
        }else{
           return $this->render_form([
                    ["type"=>"text","name"=>"name","title"=>"名称","lay-verify"=>"required"],
                    ["type"=>"textarea","name"=>"desc","title"=>"简介"],
                    ["type"=>"upload","name"=>"thumb","title"=>"图片"],
                    ["type"=>"upload","upload_type"=>"images","name"=>"slide","title"=>"轮播图"],
                    ["type"=>"editor","name"=>"content","title"=>"详情"],
                    ["type"=>"text","name"=>"out_price","title"=>"市场价格","lay-verify"=>"required"],
                    ["type"=>"text","name"=>"score","title"=>"积分","lay-verify"=>"required"],
                    ["type"=>"text","text_type"=>'number',"name"=>"stock","title"=>"库存","lay-verify"=>"required"],
                     ['type'=>'radio','name'=>'is_show', 'title'=>'是否上架','option'=>[
                        ['title'=>'上架','value'=>1],
                        ['title'=>'下架','value'=>0]
                    ]],
                    ["type"=>"text","text_type"=>'number',"name"=>"sort","title"=>"排序"],
 
           ]);
        }
    }



    /**
     * 编辑
     * @return void
     * @author 
     * date 
     */
    public function edit(){
        $id = \param_check('id');
        if(request()->isPost()) {
           $data = request()->post();
           if(ProductModel::update($data,['id'=>$id])) json_response(1,'操作成功');
           json_response(0,'操作失败');
        }else{
            $data = ProductModel::find($id);
           return $this->render_form([
                   ["type"=>"text","name"=>"name","title"=>"名称","lay-verify"=>"required"],
                    ["type"=>"textarea","name"=>"desc","title"=>"简介"],
                    ["type"=>"upload","name"=>"thumb","title"=>"图片"],
                    ["type"=>"upload","upload_type"=>"images","name"=>"slide","title"=>"轮播图"],
                    ["type"=>"editor","name"=>"content","title"=>"详情"],
                    ["type"=>"text","name"=>"out_price","title"=>"市场价格","lay-verify"=>"required"],
                    ["type"=>"text","name"=>"score","title"=>"积分","lay-verify"=>"required"],
                    ["type"=>"text","text_type"=>'number',"name"=>"stock","title"=>"库存","lay-verify"=>"required"],
                     ['type'=>'radio','name'=>'is_show', 'title'=>'是否上架','option'=>[
                        ['title'=>'上架','value'=>1],
                        ['title'=>'下架','value'=>0]
                    ]],
                    ["type"=>"text","text_type"=>'number',"name"=>"sort","title"=>"排序"],

           ],$data);
        }
    }





        /**
     * 删除
     * @return void
     * @author Sam
     * date 2021-07-01
     */
    public function del(){
        $id = param_check('id');
        if(!$ProductModel = ProductModel::get($id)) json_response(0,'不存在');
        if($ProductModel->delete()) json_response(1,'操作成功');
        json_response(0,'操作失败');
    }

    /**
     * 兑换记录
     * @return [type] [description]
     */
    public function exchange(){
    if(IS_POST) {
            $search  = request()->param();
            $where = [];
            if(isset($search["name"]) && $search["name"]!="") $where[] = ["name","=",$search["name"]];
            $db   = Exchange::with(['user','goods'])->where($where)->order('id desc');
            $list = $db->limit(paginator())->append(['status_text'])->select();
             if(count($list)){
                foreach($list as &$v){
                        $v["goods_thumb"] = table_img($v["goods_thumb"]);
                    }
                }

            __MODULE___response(0, 'success', $list, $db->count());
        }
        if(IS_EXCEL) {
            $db = Exchange::where($where)->order('id asc');
            big_array_to_csv($db, '数据导出', []);
        }
        return $this->render_table([
            "page"=>true,
            "export" => true,
            "cols" => [ 
                    ["type"=>"checkbox"],
                    ["field"=>"nickname", "title"=>"用户","align"=>"center"],
                    ["field"=>"goods_name", "title"=>"商品","align"=>"center"],
                    ["field"=>"score", "title"=>"积分","align"=>"center"],
                    ["field"=>"number", "title"=>"数量","align"=>"center"],
                    ["field"=>"status_text", "title"=>"状态","align"=>"center"],
            ],
            "toolbar_row" => [ 
                    // table_button('__MODULE__/Product/verify',"审核通过","confirm",'','','status==0'),
                    // table_button('__MODULE__/Product/refund',"审核拒绝","frame",'',['450px','300px'],'status==0'),
                    table_button('__MODULE__/Product/deliver',"发货","frame",'',['450px','300px'],'status==0')
                    ],
            "where"=>[
                    "name"=>["type"=>"input","title"=>"名称"],

            ]

            ],false);
    }


        /**
     * 发货
     * @return void
     * @author Sam
     * date 2021-12-13
     */
    public function deliver(){
        $id = \param_check('id');
        if(request()->isPost()) {
           $data = request()->post();
           $data['status'] = 1;
           if(Exchange::update($data,['id'=>$id])) json_response(1,'操作成功');
           json_response(0,'操作失败');
        }else{
            $data = Exchange::get($id);
           return $this->render_form([
                    ["type"=>"text","name"=>"express_no","title"=>"物流编号"]
           ],$data);
        }
    }


    /**
     * 审核通过 特殊场景使用 需要更改状态值 适用需要审核的兑换
     * @return [type] [description]
     */
    public function verify(){
        $id = param_check('id');
        $exchange = Exchange::find($id);
        if($exchange->status > 0) json_response(0,'状态异常');
        $exchange->status =1;
        if($exchange->save()) json_response(1,'操作成功');
        json_response(0,'操作失败');
    }

    /**
     * 审核拒绝 特殊场景使用 需要更改状态值 适用需要审核的兑换
     * @return [type] [description]
     */
    public  function refund(){
        $id = param_check('id');
        $exchange = Exchange::find($id);
        if($exchange->status > 0) json_response(0,'状态异常');
        if(request()->isPost()) {
            $reason = param_check('reason');
            try{
                $exchange->status  = 2;
                $exchange->refund_reason = $reason;
                $exchange->save();
                Exchange::in_score($exchange->uid,$exchange->score,3,$id);
                Db::commit();
            }catch(\Exception $e){
                Db::rollback();
            }
            json_response(1,'操作成功');
         }else{
            return $this->render_form([
              
                ['type'=>'textarea', 'name'=>'reason', 'title'=>'拒绝理由', 'lay-verify'=>'required'],
                
            ]);
         }
    }

    
}